package com.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.controller.vo.OpenId;
import com.controller.vo.PersonVo;
import com.dao.ICommissionerDao;
import com.dao.ICustomerDao;
import com.dao.entity.Commissioner;
import com.dao.entity.Customer;
import com.service.ICommissionerService;
import com.service.ILoginService;
import com.utils.commons.Encoder;
import com.utils.commons.HttpTookit;
import com.utils.enums.ENUM_ROLES;
import com.utils.enums.ENUM_SUCESS;
import com.utils.exceptions.ServiceException;

@Service
@ConfigurationProperties(prefix="wx")
public class LoginServiceImpl implements ILoginService {
	
	@Autowired
	private ICommissionerDao commissionerDao;
	
	@Autowired
	private ICommissionerService commissionerService;
	
	@Autowired
	private ICustomerDao customerDao;
	
	private String appID;
	
	private String appSecret;
	
	private String tokenUrl;

	@Override
	public boolean hasAccount(String account, String type) {
		switch (ENUM_ROLES.getValue(Integer.valueOf(type))){
		case ADMINISTRATOR:
			Commissioner c = commissionerDao.findByAccountAndRole(account, Integer.valueOf(type));
			if(c == null) {
				return false;
			}
			return true;
		case COMMISSIONER:
			Commissioner c1 = commissionerDao.findByAccountAndRole(account, Integer.valueOf(type));
			if(c1 == null) {
				return false;
			}
			return true;
		case AFTER:
			return false;
		case NORMAL:
			Customer cu = customerDao.findByAccount(account);
			if(cu == null) {
				return false;
			}
			return true;
		default:
			return false;
		}
	}

	@Override
	public PersonVo registerPerson(String account, String password, String openId, String type) {
		PersonVo p = null;
		switch (ENUM_ROLES.getValue(Integer.valueOf(type))){
		case ADMINISTRATOR:
			Commissioner c = new Commissioner();
			c.setAccount(account);
			c.setPassword(Encoder.MD5FreeHex(password));
			//防止申请管理员
			c.setRole(ENUM_ROLES.ADMINISTRATOR.value);
			c.setIsApproval(ENUM_SUCESS.NO.value);
			c.setOpenId(openId);
			p = new PersonVo(commissionerDao.save(c));
			break;
		case COMMISSIONER:
			Commissioner c1 = new Commissioner();
			c1.setAccount(account);
			c1.setPassword(Encoder.MD5FreeHex(password));
			c1.setRole(ENUM_ROLES.COMMISSIONER.value);
			c1.setIsApproval(ENUM_SUCESS.NO.value);
			c1.setOpenId(openId);
			p = new PersonVo(commissionerDao.save(c1));
			break;
		case AFTER:
			return null;
		case NORMAL:
			Customer cu = new Customer();
			cu.setAccount(account);
			cu.setPassword(Encoder.MD5FreeHex(password));
			cu.setOpenId(openId);
			p = new PersonVo(customerDao.save(cu));
			break;
		}
		return p;
	}

	@Override
	public PersonVo login(String account, String password) {
		Commissioner c = commissionerService.login(account, password);
		return new PersonVo(c);
	}
	
	@Override
	public PersonVo login(String account, String password, String openId, String type) {
		return typeLogin(account, Encoder.MD5FreeHex(password), openId, type);
	}

	private PersonVo typeLogin(String account, String password, String openId, String type) {
		PersonVo p;
		Integer role;
		try {
			role = Integer.valueOf(type);
		}catch (Exception e) {
			throw new ServiceException("登录类型错误");
		}
		if(!StringUtils.isEmpty(account) && !StringUtils.isEmpty(password)) {
			switch (ENUM_ROLES.getValue(Integer.valueOf(type))){
			case ADMINISTRATOR:
				Commissioner c = commissionerDao.findByAccountAndPasswordAndRole(account, password, role);
				if(c == null) {
					throw new ServiceException("账号或密码错误");
				}
				if(c.getIsApproval() == null || ENUM_SUCESS.NO.value.equals(c.getIsApproval())) {
					throw new ServiceException("未通过审批");
				}
				p = new PersonVo(c);
				return p;
			case COMMISSIONER:
				Commissioner c1 = commissionerDao.findByAccountAndPasswordAndRole(account, password, role);
				if(c1 == null) {
					throw new ServiceException("账号或密码错误");
				}
				p = new PersonVo(c1);
				return p;
			case AFTER:
				throw new ServiceException("查无此人");
			case NORMAL:
				Customer cu = customerDao.findByAccountAndPassword(account, password);
				if(cu == null) {
					throw new ServiceException("账号或密码错误");
				}
				p = new PersonVo(cu);
				return p;
			default:
				throw new ServiceException("查无此人");
			}
		}
		switch (ENUM_ROLES.getValue(Integer.valueOf(type))){
		case ADMINISTRATOR:
			Commissioner c = commissionerDao.findByOpenIdAndRole(openId, role);
			if(c == null) {
				throw new ServiceException("查无此人");
			}
			if(c.getIsApproval() == null || ENUM_SUCESS.NO.value.equals(c.getIsApproval())) {
				throw new ServiceException("未通过审批");
			}
			p = new PersonVo(c);
			return p;
		case COMMISSIONER:
			Commissioner c1 = commissionerDao.findByOpenIdAndRole(openId, role);
			if(c1 == null) {
				throw new ServiceException("查无此人");
			}
			if(c1.getIsApproval() == null || ENUM_SUCESS.NO.value.equals(c1.getIsApproval())) {
				throw new ServiceException("未通过审批");
			}
			p = new PersonVo(c1);
			return p;
		case AFTER:
			throw new ServiceException("查无此人");
		case NORMAL:
			Customer cu = customerDao.findByOpenId(openId);
			if(cu == null) {
				throw new ServiceException("查无此人");
			}
			p = new PersonVo(cu);
			return p;
		default:
			throw new ServiceException("查无此人");
		}
	}

	@Override
	public void forgetPassword(String account, String password, String type) {
		switch (ENUM_ROLES.getValue(Integer.valueOf(type))){
		case ADMINISTRATOR:
			Commissioner c = commissionerDao.findByAccountAndRole(account, Integer.valueOf(type));
			if(c == null) {
				throw new ServiceException("账号未注册");
			}
			c.setPassword(Encoder.MD5FreeHex(password));
			commissionerDao.save(c);
			break;
		case COMMISSIONER:
			Commissioner c1 = commissionerDao.findByAccountAndRole(account, Integer.valueOf(type));
			if(c1 == null) {
				throw new ServiceException("账号未注册");
			}
			c1.setPassword(Encoder.MD5FreeHex(password));
			commissionerDao.save(c1);
			break;
		case AFTER:
			throw new ServiceException("查无此人");
		case NORMAL:
			Customer cu = customerDao.findByAccount(account);
			if(cu == null) {
				throw new ServiceException("账号未注册");
			}
			cu.setPassword(Encoder.MD5FreeHex(password));
			customerDao.save(cu);
			break;
		default:
			throw new ServiceException("查无此人");
		}
	}

	@Override
	public OpenId getAccess_token(String code) throws IOException {
		Map<String, Object> pramsMap = new HashMap<String, Object>();
		pramsMap.put("appid", appID);
		pramsMap.put("secret", appSecret);
		pramsMap.put("grant_type", "authorization_code");
		pramsMap.put("code", code);
		String str = HttpTookit.connectionUrl(tokenUrl, "GET", pramsMap);
		return JSON.parseObject(str, OpenId.class);
	}


	
	
	public String getAppID() {
		return appID;
	}

	public void setAppID(String appID) {
		this.appID = appID;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getTokenUrl() {
		return tokenUrl;
	}

	public void setTokenUrl(String tokenUrl) {
		this.tokenUrl = tokenUrl;
	}
	
}
